package com.jd.mti.beetl.test.load;

import com.jd.mti.beetl.test.DataUtil;
import com.jd.mti.beetl.test.controller.RenderController;
import com.jd.mti.beetl.test.io.CharStream;
import com.jd.mti.beetl.test.io.NoneStream;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.TemplateException;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import java.io.IOException;
import java.util.*;

/**
 * @Author sizegang
 * @Date 2021/11/14 20:10
 * @Version 1.0
 */
public class RenderBeetl extends  LoadPare{

    public static GroupTemplate gt = null;

    public static void loadGtConfig() throws IOException {
        ClasspathResourceLoader resourceLoader = new MyClasspathResourceLoader("/");
        Configuration cfg = Configuration.defaultConfiguration();
        cfg.setStatementEnd(null);
        cfg.setDirectByteOutput(true);

        gt = new GroupTemplate(resourceLoader, cfg);
    }

    public static void loadBeetl() {
        try {
            loadGtConfig();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map map = DataUtil.dataManMap;

        long startTime = System.currentTimeMillis();

        for (int i = 0; i < 1000; i++) {
            try {
                Template template = gt.getTemplate("/templates/beetl.html");
                template.binding(map);
                NoneStream stream = new NoneStream();
                template.renderTo(stream);
            }catch (Exception ex){

            }
        }

        long cost = System.currentTimeMillis() - startTime;
        System.out.println("10000次渲染, beetl 消耗时间" +cost + "毫秒");

    }






    public static void main(String[] args) {
        loadBeetl();
    }




}
